package algorithm.offer;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Offer40 {

  public static void main(String[] args) {
    int[] arr = {1, 2, 3};
    int k = 4;
    if (k == 0)
      return;
    //求最小 用大顶堆
    // 大顶堆
    PriorityQueue<Integer> p = new PriorityQueue(new Comparator<Integer>() {
      @Override
      public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
      }
    });

    for (int i : arr) {
      if (p.size() == k) {
        Integer peek = p.peek();
        if (i < peek) {
          p.poll();
          p.add(i);
        }
      } else {
        p.add(i);
      }
    }

    int len = p.size();
    int[] res = new int[len];

    System.out.println(len);
    for (int i = 0; i < len; i++) {
      res[i] = p.poll();
    }
    System.out.println(Arrays.toString(res));
//    return res;
  }

}
